home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c14 / Counter5.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  6.4 KB  |  191 lines

  1. //: Counter5.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Adjusting the priorities of threads
  50. import java.awt.*;
  51. import java.awt.event.*;
  52. import java.applet.*;
  53.  
  54. class Ticker2 extends Thread {
  55.   private Button 
  56.     b = new Button("Toggle"),
  57.     incPriority = new Button("up"),
  58.     decPriority = new Button("down");
  59.   private TextField 
  60.     t = new TextField(10),
  61.     pr = new TextField(3); // Display priority
  62.   private int count = 0;
  63.   private boolean runFlag = true;
  64.   public Ticker2(Container c) {
  65.     b.addActionListener(new ToggleL());
  66.     incPriority.addActionListener(new UpL());
  67.     decPriority.addActionListener(new DownL());
  68.     Panel p = new Panel();
  69.     p.add(t);
  70.     p.add(pr);
  71.     p.add(b);
  72.     p.add(incPriority);
  73.     p.add(decPriority);
  74.     c.add(p);
  75.   }
  76.   class ToggleL implements ActionListener {
  77.     public void actionPerformed(ActionEvent e) {
  78.       runFlag = !runFlag;
  79.     }
  80.   }
  81.   class UpL implements ActionListener {
  82.     public void actionPerformed(ActionEvent e) {
  83.       int newPriority = getPriority() + 1;
  84.       if(newPriority > Thread.MAX_PRIORITY)
  85.         newPriority = Thread.MAX_PRIORITY;
  86.       setPriority(newPriority);
  87.     }
  88.   }
  89.   class DownL implements ActionListener {
  90.     public void actionPerformed(ActionEvent e) {
  91.       int newPriority = getPriority() - 1;
  92.       if(newPriority < Thread.MIN_PRIORITY)
  93.         newPriority = Thread.MIN_PRIORITY;
  94.       setPriority(newPriority);
  95.     }
  96.   }
  97.   public void run() {
  98.     while (true) {
  99.       if(runFlag) {
  100.         t.setText(Integer.toString(count++));
  101.         pr.setText(
  102.           Integer.toString(getPriority()));
  103.       }
  104.       yield();
  105.     }
  106.   }
  107. }
  108.  
  109. public class Counter5 extends Applet {
  110.   private Button 
  111.     start = new Button("Start"),
  112.     upMax = new Button("Inc Max Priority"),
  113.     downMax = new Button("Dec Max Priority");
  114.   private boolean started = false;
  115.   private static final int SIZE = 10;
  116.   private Ticker2[] s = new Ticker2[SIZE];
  117.   private TextField mp = new TextField(3);
  118.   public void init() {
  119.     for(int i = 0; i < s.length; i++)
  120.       s[i] = new Ticker2(this);
  121.     add(new Label("MAX_PRIORITY = "
  122.       + Thread.MAX_PRIORITY));
  123.     add(new Label("MIN_PRIORITY = "
  124.       + Thread.MIN_PRIORITY));
  125.     add(new Label("Group Max Priority = "));
  126.     add(mp); 
  127.     add(start);
  128.     add(upMax); add(downMax);
  129.     start.addActionListener(new StartL());
  130.     upMax.addActionListener(new UpMaxL());
  131.     downMax.addActionListener(new DownMaxL());
  132.     showMaxPriority();
  133.     // Recursively display parent thread groups:
  134.     ThreadGroup parent = 
  135.       s[0].getThreadGroup().getParent();
  136.     while(parent != null) {
  137.       add(new Label(
  138.         "Parent threadgroup max priority = "
  139.         + parent.getMaxPriority()));
  140.       parent = parent.getParent();
  141.     }
  142.   }
  143.   public void showMaxPriority() {
  144.     mp.setText(Integer.toString(
  145.       s[0].getThreadGroup().getMaxPriority()));
  146.   }
  147.   class StartL implements ActionListener {
  148.     public void actionPerformed(ActionEvent e) {
  149.       if(!started) {
  150.         started = true;
  151.         for(int i = 0; i < s.length; i++)
  152.           s[i].start();
  153.       }
  154.     }
  155.   }
  156.   class UpMaxL implements ActionListener {
  157.     public void actionPerformed(ActionEvent e) {
  158.       int maxp = 
  159.         s[0].getThreadGroup().getMaxPriority();
  160.       if(++maxp > Thread.MAX_PRIORITY)
  161.         maxp = Thread.MAX_PRIORITY;
  162.       s[0].getThreadGroup().setMaxPriority(maxp);
  163.       showMaxPriority();
  164.     }
  165.   }
  166.   class DownMaxL implements ActionListener {
  167.     public void actionPerformed(ActionEvent e) {
  168.       int maxp = 
  169.         s[0].getThreadGroup().getMaxPriority();
  170.       if(--maxp < Thread.MIN_PRIORITY)
  171.         maxp = Thread.MIN_PRIORITY;
  172.       s[0].getThreadGroup().setMaxPriority(maxp);
  173.       showMaxPriority();
  174.     }
  175.   }
  176.   public static void main(String[] args) {
  177.     Counter5 applet = new Counter5();
  178.     Frame aFrame = new Frame("Counter5");
  179.     aFrame.addWindowListener(
  180.       new WindowAdapter() {
  181.         public void windowClosing(WindowEvent e) {
  182.           System.exit(0);
  183.         }
  184.       });
  185.     aFrame.add(applet, BorderLayout.CENTER);
  186.     aFrame.setSize(300, 600);
  187.     applet.init();
  188.     applet.start();
  189.     aFrame.setVisible(true);
  190.   }
  191. } ///:~